package com.un.ebs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.product.domain.*;
import com.un.ebs.product.dto.*;
import com.un.ebs.product.mapper.*;
import com.un.ebs.product.service.ProductClassAttrService;
import com.un.ebs.product.service.ProductClassOrderAttrService;
import com.un.ebs.product.service.ProductClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/productClassOrderAttr")
@Api(value = "product-class-order-attr", description = "品类订单属性")
public class ProductClassOrderAttrController {

    @Autowired
    private ProductClassOrderAttrService service;

    @Autowired
    private ProductClassService productClassService;

    @Autowired
    private ProductClassOrderAttrExtMapper productClassOrderAttrExtMapper;

    @Autowired
    private ProductClassOrderAttrGroupMapper productClassOrderAttrGroupMapper;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody ProductClassOrderAttrAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody ProductClassOrderAttrUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 ProductClassAttr")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list/{classId}")
    @ApiOperation(value = "list", notes = "")
    public WebResult<List<ProductClassOrderAttrVO>> list(@PathVariable("classId") String classId) {

        List<String> parentIds = productClassService.queryParentIds(classId);

        //查询属性分组
        LambdaQueryWrapper<ProductClassOrderAttrGroup> lqGroup = Wrappers.lambdaQuery();
        if (!StringUtils.isNullOrEmpty(parentIds)) {
            lqGroup.in(ProductClassOrderAttrGroup::getClassId, parentIds);
        }

        lqGroup.eq(ProductClassOrderAttrGroup::getIsDelete, false);
        lqGroup.orderByAsc(ProductClassOrderAttrGroup::getSort);
        List<ProductClassOrderAttrGroup> groups = productClassOrderAttrGroupMapper.selectList(lqGroup);

        //查询类别属性
        LambdaQueryWrapper<ProductClassOrderAttrExt> lq = Wrappers.lambdaQuery();
        if (!StringUtils.isNullOrEmpty(parentIds)) {
            lq.in(ProductClassOrderAttrExt::getClassId, parentIds);
        }
        lq.eq(ProductClassOrderAttrExt::getIsDelete, false);
        lq.orderByAsc(ProductClassOrderAttrExt::getGroupId);
        lq.orderByAsc(ProductClassOrderAttrExt::getSort);
        List<ProductClassOrderAttrExt> attrs = productClassOrderAttrExtMapper.selectList(lq);

        List<ProductClassOrderAttrVO> result = new ArrayList<>();
        for (ProductClassOrderAttrGroup group : groups) {
            ProductClassOrderAttrVO vo = new ProductClassOrderAttrVO();
            vo.setGroupId(group.getId());
            vo.setGroupName(group.getGroupName());
            vo.setGroupSort(group.getSort());
            vo.setAttrs(new ArrayList<>());
            vo.setAttrs(attrs.stream().filter(s -> group.getId().equals(s.getGroupId())).collect(Collectors.toList()));
            result.add(vo);
        }

        return WebUtils.success(result);
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 ProductClassAttr")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 ProductClassAttr 的序号")
    public WebResult changeSort(@RequestBody List<SortChangeDto> req) {
        service.changeSort(req);
        return WebUtils.success();
    }

}